<?php
namespace Home\Controller;
use Think\Controller;
header("Content-type:text/html; charset=utf-8");
class LoginController extends Controller {
    public function index(){
    	$redirect_url="http://wx.weiyinstudio.com?backurl=http://www.emall.org/index.php/Home/Login/login?";
    	$redirect_url=urlencode($redirect_url);
    	$redirect_url="https://api.weibo.com/oauth2/authorize?client_id=3911964253&response_type=code&redirect_uri={$redirect_url}";
    	
    	$this->assign('link',$redirect_url);
        $this->display();
    }
    public function login(){
    	$code=$_GET['code'];
    	$link="https://api.weibo.com/oauth2/access_token?client_id=3911964253&client_secret=7ebbd60b716a6163e8108d9e12109de8&grant_type=authorization_code&redirect_uri=http%3a%2f%2fwx.weiyinstudio.com&code={$code}";
    	$data=post($link);
    	$data=json_decode($data,true);
        
    	$link="https://api.weibo.com/2/users/show.json?access_token={$data['access_token']}&uid={$data['uid']}";
    	$row=get($link,true);
    	$data=json_decode($row,true);
    	$db=M('mallLogin');
    	$exist=$db->where("uid='{$data['id']}'")->find();
    	if (empty($exist)) {
    		$val['name']=$data['name'];
    		$val['image']=$data['profile_image_url'];
    		$val['uid']=$data['id'];
    		$id=$db->data($val)->add();
    		$_SESSION['uname']= $data['name'];
    		$_SESSION['uid']= $id;

    	}else{
    		$_SESSION['uname']=$exist['name'];
    		$_SESSION['uid']= $exist['id'];
            

    	}
    		
    	$this->redirect('Home/Index/index');
    }
}